package com.ytjj.qmyx.data.amqp.consumer;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.data.config.SupplyApiConfig;
import com.ytjj.qmyx.data.mapper.StatisticsMapper;
import com.ytjj.qmyx.data.model.ReturnOrderStatisticsRequest;
import com.ytjj.qmyx.data.model.response.ReturnOrderStatisticsResponse;
import com.ytjj.qmyx.mall.mapper.ComplaintsRecordsMapper;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 */
@Slf4j
@Component
@RefreshScope
public class ReturnOrderStatisticsListener {

    @Resource
    private StatisticsMapper statisticsMapper;

    @Resource
    private SupplyApiConfig supplyApiConfig;


    /**
     * 统计售后订单
     */
    @RabbitListener(queuesToDeclare = @Queue(value = QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, durable = "true"))
    public void getReturnOrderStatistics(String queryParams, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        log.info("====收到一条查询售后订单的请求的消息{}====", queryParams);
        ReturnOrderStatisticsRequest request = JSON.parseObject(queryParams, ReturnOrderStatisticsRequest.class);
        List<ReturnOrderStatisticsResponse> afterProductDate = statisticsMapper.getAfterProductDate(request);
        List<ReturnOrderStatisticsResponse> productComplaintsStatistics = statisticsMapper.getProductComplaintsStatistics(request.getStartTime(), request.getEndTime());
        Map<Integer, ReturnOrderStatisticsResponse> complaintsMap = productComplaintsStatistics.stream().collect(Collectors.toMap(ReturnOrderStatisticsResponse::getSupplyProductId, Function.identity()));
        afterProductDate.stream().forEach(e -> {
            e.setWxComplaintsNum(0);
            e.setAliMiniComplaintsNum(0);
            e.setAlipayComplaintsNum(0);
            if (!CollectionUtils.isEmpty(complaintsMap)) {
                ReturnOrderStatisticsResponse complaintsEntity = complaintsMap.get(e.getSupplyProductId());
                if (complaintsEntity != null) {
                    e.setWxComplaintsNum(complaintsEntity.getWxComplaintsNum());
                    e.setAliMiniComplaintsNum(complaintsEntity.getAliMiniComplaintsNum());
                    e.setAlipayComplaintsNum(complaintsEntity.getAlipayComplaintsNum());
                }
            }
        });
        HttpClientUtil.doPostJson(supplyApiConfig.getUrl()+"/returnGoods/recordRefundOrderCallback/"+request.getMarkId(),JSON.toJSONString(afterProductDate),supplyApiConfig.getHeadsMap());
    }
}
